package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO pageInfo) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getPageSize());
        Page<Category> res =  categoryMapper.pageQuery(pageInfo);
        return new PageResult(res.getTotal(),res);
    }

    @Override
    public void addCategoryType(CategoryDTO category) {
        Category cat = new Category();
        BeanUtils.copyProperties(category,cat);
        cat.setStatus(StatusConstant.ENABLE);
//        cat.setCreateTime(LocalDateTime.now());
//        cat.setUpdateTime(LocalDateTime.now());
//        cat.setCreateUser(BaseContext.getCurrentId());
//        cat.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.inset(cat);
    }

    @Override
    public void disbale(Integer status, Long id) {
        Category category = categoryMapper.queryById(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    @Override
    public void updateName(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> queryList(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }
}
